package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.material_report.MaterialReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IMaterialReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static java.util.Comparator.comparing;

@Slf4j
@Service
public class MaterialReportServiceImpl implements IMaterialReportService {


    public List<MaterialReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("A.apply_timestamp,");
        sqlVo.appendSql("A.apply_timestamp as apply_date,");
        sqlVo.appendSql("A.apply_timestamp as apply_time,");
        sqlVo.appendSql("A.fab_id_fk,");
        sqlVo.appendSql("b_1.data_desc as fab_id,");
        sqlVo.appendSql("A.apply_stat,");
        sqlVo.appendSql("A.mtrl_prod_id_fk,");
        sqlVo.appendSql("b_2.mtrl_name,");
        sqlVo.appendSql("b_2.mtrl_spec,");
        sqlVo.appendSql("b_2.mtrl_unit,");
        sqlVo.appendSql("A.relate_mtrl_qty,");
        sqlVo.appendSql("A.apply_usr,");
        sqlVo.appendSql("G.usr_name as apply_usr_name,");
        sqlVo.appendSql("A.requisition_id,");
        sqlVo.appendSql("A.agree_timestamp,");
        sqlVo.appendSql("A.comp_timestamp,");
        sqlVo.appendSql("A.evt_note");
        sqlVo.appendSql("FROM ret_mtrl_aply A");
        sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'FBID') b_1 ON A.FAB_ID_FK = b_1.data_ext");
        sqlVo.appendSql("LEFT JOIN bis_mtrl b_2 on A.mtrl_prod_id_fk = b_2.mtrl_code");
        sqlVo.appendSql("LEFT JOIN bis_user G ON A.apply_usr = G.usr_id");
        sqlVo.appendSql("WHERE");
        sqlVo.appendSql("A.APPLY_STAT='COMP'");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
        if (StringUtils.isNotEmpty(conditions.getMtrl_prod_id_fk())) sqlVo.appendSql("AND A.MTRL_PROD_ID_FK in" + jointInSql(conditions.getMtrl_prod_id_fk()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.EVT_TIMESTAMP >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.EVT_TIMESTAMP <= ?", conditions.getEnd_time());
        sqlVo.appendSql("ORDER BY fab_id, mtrl_prod_id_fk, apply_timestamp desc");

        List<MaterialReportGridOut> resultDataList = mySql_QueryRunner_QueryListBySql(sqlVo, MaterialReportGridOut.class);

        //by mtrlProdId的汇总数据
        if ("SUMMARY".equals(conditions.getSummary_or_detail())) {
            List<MaterialReportGridOut> by_mtrlPordId_gridOut_list = new ArrayList<>();
            resultDataList.stream().collect(Collectors.groupingBy(data -> data.getMtrl_prod_id_fk())).forEach((mtrl_prod_id, by_mtrlProdId_list) -> {
                MaterialReportGridOut gridOut = new MaterialReportGridOut();
                //gridOut.setApply_date(by_mtrlProdId_list.stream().collect(minBy(comparing(data -> data.getApply_date()))).orElseGet(MaterialReportGridOut::new).getApply_date());
                //gridOut.setFab_id(by_mtrlProdId_list.get(0).getFab_id());
                //gridOut.setFab_id_fk(by_mtrlProdId_list.get(0).getFab_id_fk());
                gridOut.setApply_stat(by_mtrlProdId_list.get(0).getApply_stat());
                gridOut.setMtrl_prod_id_fk(by_mtrlProdId_list.get(0).getMtrl_prod_id_fk());
                gridOut.setMtrl_name(by_mtrlProdId_list.get(0).getMtrl_name());
                gridOut.setMtrl_spec(by_mtrlProdId_list.get(0).getMtrl_spec());
                gridOut.setMtrl_unit(by_mtrlProdId_list.get(0).getMtrl_unit());
                gridOut.setRelate_mtrl_qty(String.valueOf(by_mtrlProdId_list.stream().mapToDouble(data -> Double.parseDouble(data.getRelate_mtrl_qty())).sum()));
                by_mtrlPordId_gridOut_list.add(gridOut);
            });
            return by_mtrlPordId_gridOut_list;
        }

        return resultDataList;
    }



}
